const Service = require('egg').Service

class LoginService extends Service {
	constructor(ctx) {
		super(ctx)
	}
	async index(body) {
		const { username, password, captcha } = body
		const { captchaCode } = this.ctx.session
		// 验证码验证
		if (captcha === captchaCode) {
			const encryptPassword = this.ctx.helper.encrypt(password)
			const hasOneSameName = await this.ctx.model.User.findOne({
				where: { username, password: encryptPassword },
			})
			if (hasOneSameName) {
				if (hasOneSameName.status == 1) {
					return {
						status: 'success',
						id: hasOneSameName.id,
					}
				} else {
					return {
						status: '该用户已被禁用，请联系管理员',
					}
				}
			} else {
				return {
					status: '用户名或密码错误',
				}
			}
		} else {
			return { status: '验证码错误或已失效' }
		}
	}
}

module.exports = LoginService
